package com.sunday.pinxinghui.model.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.adapter.CommenAdapter;
import com.sunday.common.adapter.ViewHolder;
import com.sunday.common.base.BaseFragment;
import com.sunday.common.model.ResultDO;
import com.sunday.common.widgets.CircleImageView;
import com.sunday.common.widgets.NoScrollGridView;
import com.sunday.common.widgets.NoScrollListview;
import com.sunday.common.widgets.ptr.PtrClassicFrameLayout;
import com.sunday.common.widgets.ptr.PtrDefaultHandler;
import com.sunday.common.widgets.ptr.PtrFrameLayout;
import com.sunday.common.widgets.ptr.PtrHandler;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.base.BaseApplication;
import com.sunday.pinxinghui.entity.LifeData;
import com.sunday.pinxinghui.model.lifecircle.SearchShopActivity;
import com.sunday.pinxinghui.model.lifecircle.ShopDetailActivity;
import com.sunday.pinxinghui.model.lifecircle.ShopListActivity;
import com.sunday.pinxinghui.network.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/8/3.
 */

public class LiveCircleFragment extends BaseFragment {

    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.classify_recyclerview)
    NoScrollGridView classifyRecyclerview;
    @Bind(R.id.recmend_layout)
    LinearLayout recmendLayout;
    @Bind(R.id.recmend_list)
    NoScrollListview recmendList;
    @Bind(R.id.ptr_frame)
    PtrClassicFrameLayout ptrFrame;
    @Bind(R.id.scrollView)
    ScrollView scrollView;
    @Bind(R.id.right_btn)
    ImageView rightBtn;


    private CommenAdapter<LifeData.CatsBean> catAdapter;
    private List<LifeData.CatsBean> catsBeanList = new ArrayList<>();
    private List<LifeData.ShopsBean> shopList = new ArrayList<>();
    private CommenAdapter<LifeData.ShopsBean> shopAdapter;


    public static LiveCircleFragment newInstance() {
        LiveCircleFragment fragment = new LiveCircleFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.live_circle_fragment, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        leftBtn.setVisibility(View.GONE);
        titleView.setText("生活分类");
        titleView.setFocusable(true);
        titleView.setFocusableInTouchMode(true);
        titleView.requestFocus();
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setImageResource(R.mipmap.search);
        initRecyclerViews();
        getLifeData();
    }

    private void initRecyclerViews() {
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(mContext);
        layoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        catAdapter = new CommenAdapter<LifeData.CatsBean>(mContext, catsBeanList, R.layout.list_life_cat_item) {
            @Override
            public void convert(ViewHolder holder, LifeData.CatsBean catsBean, int position) {
                CircleImageView imageView = holder.getView(R.id.category_img);
                Glide.with(mContext)
                        .load(catsBean.getImage())
                        .error(R.mipmap.ic_default)
                        .into(imageView);
                ((TextView) holder.getView(R.id.category_name)).setText(catsBean.getName());
            }
        };
        classifyRecyclerview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent = new Intent(mContext, ShopListActivity.class);
                LifeData.CatsBean catsBean = catsBeanList.get(position);
                intent.putExtra("catName", catsBean.getName());
                intent.putExtra("catId", catsBean.getId());
                startActivity(intent);
            }
        });
        classifyRecyclerview.setAdapter(catAdapter);

        shopAdapter = new CommenAdapter<LifeData.ShopsBean>(mContext, shopList, R.layout.list_life_shop_item) {
            @Override
            public void convert(ViewHolder helper, final LifeData.ShopsBean item, int position) {
                ImageView imgView = helper.getView(R.id.shop_img);
                Glide.with(mContext)
                        .load(item.getImage())
                        .error(R.mipmap.ic_default)
                        .into(imgView);
                ((TextView) helper.getView(R.id.shop_title)).setText(item.getName());
                ((TextView) helper.getView(R.id.shop_desc)).setText(item.getDesc());
                ((TextView) helper.getView(R.id.shop_addr)).setText("【" + item.getArea() + "】");
                ((RelativeLayout) helper.getView(R.id.layout)).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        intent = new Intent(mContext, ShopDetailActivity.class);
                        intent.putExtra("shopId", item.getId());
                        startActivity(intent);
                    }
                });
            }
        };
        recmendList.setAdapter(shopAdapter);

        ptrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getLifeData();
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, scrollView, header);
            }
        });


    }

    private void getLifeData() {
        Call<ResultDO<LifeData>> call = ApiClient.getApiAdapter().getLifeData(BaseApplication.getInstance().getMember().getId());
        call.enqueue(new Callback<ResultDO<LifeData>>() {
            @Override
            public void onResponse(Call<ResultDO<LifeData>> call, Response<ResultDO<LifeData>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                ptrFrame.refreshComplete();
                if (response.body().getCode() == 0) {
                    catsBeanList.clear();
                    catsBeanList.addAll(response.body().getResult().getCats());
                    shopList.clear();
                    shopList.addAll(response.body().getResult().getShops());
                    catAdapter.notifyDataSetChanged();
                    shopAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<LifeData>> call, Throwable t) {
                ptrFrame.refreshComplete();
            }
        });
    }

    @OnClick(R.id.right_btn)
    void searchShop(){
        intent=new Intent(mContext, SearchShopActivity.class);
        startActivity(intent);
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
